pow() ceil() log2() error?

28 views
Skip to first unread message

Andrew Gill

unread,
Apr 14, 2025, 6:02:29 AMApr 14
to MiniZinc
Hi - I'm trying to minimize "the smallest power of 2 strictly greater than an integer variable". 

When I use (version 2.9.2, and where n is an array of var int):
var int: runs;

constraint runs=pow(2,ceil(log2(n[num_factors])+0.001));

solve minimize runs;


I get: 

Error: evaluation error: comprehension iterates over an infinite set

/home/gilla/file.mzn:36.12-55

  in binary '=' operator expression

  in call 'pow'

/snap/minizinc/1136/share/minizinc/std/stdlib/stdlib_math.mzn:664.3-674.7

  in if-then-else expression

/snap/minizinc/1136/share/minizinc/std/stdlib/stdlib_math.mzn:669.5-82

  in if-then-else expression

  in call 'pow_t'

/snap/minizinc/1136/share/minizinc/std/stdlib/stdlib_internal.mzn:2867.3-2871.8

  in let expression

/snap/minizinc/1136/share/minizinc/std/stdlib/stdlib_internal.mzn:2870.16-31

  in call 'int_pow'

/snap/minizinc/1136/share/minizinc/gecode/redefinitions.mzn:82.5-87.25

  in let expression

/snap/minizinc/1136/share/minizinc/gecode/redefinitions.mzn:83.9-86.9

  in variable declaration for 'pow_table'

  in array comprehension expression


But it seems to work if I instead do (since its a monotonic function):

solve minimize n[num_factors];

output ["Solution:\n", "Sequencies = ", show(n),"\nruns = ", show(pow(2,ceil(log2(n[num_factors])+0.001)))];


I would prefer the first version, so I'm not sure why it doesn't work (but the latter does).


Any advice gratefully accepted!

guido.tack

unread,
Apr 14, 2025, 7:24:46 AMApr 14
to MiniZinc
Hi,

The log2 function currently doesn't produce a result with suitable bounds, so the MiniZinc compiler doesn't know how to implement the pow function on an infinite range. We need to fix this in MiniZinc, thanks for pointing it out.

However, in this case, the compiler won't be able to find out that the log2, ceil and pow functions are all monotonic, which means that minimising n[num_factors] is in fact the same as minimising runs. We have no plans (in the near future) to add this kind of analysis. I would therefore expect the solving to be much more efficient on your workaround version, even once we fix the issue with log2.

Cheers,
Guido

Andrew Gill

unread,
Apr 14, 2025, 7:27:29 AMApr 14
to MiniZinc
Thanks Guido! Good to know I wasn't doing something silly.

Andrew Gill

unread,
Apr 16, 2025, 11:12:48 AMApr 16
to MiniZinc
UPDATE: Mostly as a learning opportunity for me, but I found a way around using log2 (actually my son prodded me in the right direction!):

constraint N = pow(2, arg_max([n[num_factors] < pow(2,b) | b in 0..15]) - 1);
solve minimize N;

Reply all
Reply to author
Forward
0 new messages